﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace LiveShopvnext.Commodity
{
    public class ManualProuctAppService : ApplicationService, IManualProuctAppService
    {
        private readonly IRepository<Commodity.Product, int> _product;
        private readonly IRepository<Commodity.Category, int> _category;
        private readonly IRepository<Imges.Image, int> _images;

        public ManualProuctAppService
            (
                IRepository<Commodity.Product, int> product, 
                IRepository<Commodity.Category, int> category,
                IRepository<Imges.Image, int> images
            )
        {
            _category = category;
            _product = product;
            _images = images;
        }

        #region 分类和产品联查
        [HttpGet]
        public PagedResultDto<ProductIdDto> GetProductDtos(ProductListDto input)
        {
            var dtos = (
                    from p in _product
                    join pt in _category
                    on p.productTypeId equals pt.Id
                    select new ProductIdDto
                    {
                        Id = p.Id,
                        productTypeId = pt.Id,
                        productAttrName = p.productAttrName,
                        productDetails = p.productDetails,
                        productDisplay = p.productDisplay,
                        productName = p.productName,
                        productSalePrice = p.productSalePrice,
                        productSpeciId = p.productSpeciId,
                        productSubtitle = p.productSubtitle,
                        productWeight = p.productWeight,
                        categoryName=pt.categoryName,
                        productSPromotion=p.productSPromotion,
                        productState=p.productState,
                        productSum= p.productSum
                        
                    }
                );
            
            var list= dtos
                .WhereIf(!string.IsNullOrEmpty(input.productName), c => c.productName.Contains(input.productName))             //产品名称
                .WhereIf(!string.IsNullOrEmpty(input.productSubtitle), c => c.productSubtitle.Contains(input.productSubtitle)) //产品副名称
                .WhereIf(input.productTypeId > 0, c => c.productTypeId == input.productTypeId)                                 //产品分类主键
                .WhereIf(input.productState > 0, c => c.productState == input.productState)                                    //是否上架
                .WhereIf(input.productDisplay > 0, c => c.productDisplay == input.productDisplay).ToList();                    //逻辑删除


            var totalCount= list.Count();

            list = list
            .Skip(input.SkipCount)
            .Take(input.MaxResultCount).ToList();

            return new PagedResultDto<ProductIdDto>(totalCount, list);
        }
        #endregion

        #region 逻辑删除
        [HttpPut]
        public ProductDto UpdateState(int id)
        {
            var dtos = _product.FirstOrDefault(x => x.Id == id);

            if (dtos.productDisplay == 1)
            {
                dtos.productDisplay = 2;
            }
            else if (dtos.productDisplay == 2)
            {
                dtos.productDisplay = 1;
            }

            return ObjectMapper.Map<Product, ProductDto>(dtos);
        }
        #endregion

        #region 图片和产品联查和分类
        /// <summary>
        /// 图片和产品联查
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet]
        public List<ProductIdDto> ListProductDto(int Id=0)
        {
            var Product = from a in _images
                          join b in _product
                          on a.imageSId equals b.Id
                          join c in _category
                          on b.productTypeId equals c.Id
                          select new ProductIdDto
                          {
                              Id = b.Id,
                              productTypeId = c.Id,
                              productAttrName = b.productAttrName,
                              productDetails = b.productDetails,
                              productDisplay = b.productDisplay,
                              productName = b.productName,
                              productSalePrice = b.productSalePrice,
                              productSpeciId = b.productSpeciId,
                              productSubtitle = b.productSubtitle,
                              productWeight = b.productWeight,
                              categoryName = c.categoryName,
                              productSPromotion = b.productSPromotion,
                              productState = b.productState,
                              imageURL = a.imageURL,
                              imagedelete=a.imagedelete,
                              productSum = b.productSum

                          };

            var list = Product.Where(c => c.productDisplay == 1&&c.imagedelete==1&&c.productState!=2)
                        .WhereIf(Id>0,c=>c.Id==Id).ToList();

            return list;
        }
        #endregion

        #region 修改产品库存
        /// <summary>
        /// 修改产品库存
        /// </summary>
        /// <param name="inven">执行加减库存</param>
        /// <param name="productid">加减库存产品id</param>
        /// /// <param name="productid">加减库存数量</param>
        /// <returns></returns>
        [HttpPut]
        public int inventory(List<InventoryDto> inventories) {

            List<Product> list = new List<Product>();

            foreach (var item in inventories)
            {
                var products = item;
                var product = _product.FirstOrDefault(c => c.Id == products.productid);

                if (product.productSum<0)
                {
                    return 0;
                }
                if (item.inven == 1) //加库存
                {
                    product.productSum += item.productSum;
                }
                else if (item.inven == 2)//减库存
                { 
                    product.productSum -= item.productSum;
                }
                list.Add(product);
            }
            _product.UpdateManyAsync(list);
            return 1;

        }
        #endregion


    }
}
